﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using System.IO;
using System.Windows;
using System.Runtime.Serialization;
using System.Windows.Controls;
using System.ComponentModel;

namespace music_control
{
    public class Controller : INotifyPropertyChanged
    {
        private ObservableCollection<IMusicPlugin> MusicPlugins;

        private KeyObserver _keyobserver;
        public KeyObserver Keyobserver
        {
            get
            {
                if (_keyobserver == null)
                    _keyobserver = new KeyObserver();

                return this._keyobserver;
            }

            set
            {
                this._keyobserver = value;
            }
        }

        public Controller()
        {
            MusicPlugins = new ObservableCollection<IMusicPlugin>();

            //Add new Plugins here
            MusicPlugins.Add(new MusicPluginChrome());

            Keyobserver.KeyPressed += new KeyObserver.KeyPressedEventHandler(Keyobserver_KeyPressed);

        }

        void Keyobserver_KeyPressed(object sender, PlayerKeyPressedEventArgs e)
        {
            if (e.PressedKey == PlayerControlEnum.StartStop)
            {
                StartStopMusic();
            }
            else if(e.PressedKey == PlayerControlEnum.NextTrack)
            {
                NextTrack();
            }
            else if(e.PressedKey == PlayerControlEnum.PreviouseTrack)
            {
                PreviouseTrack();
            }
        }


        public ObservableCollection<UserControl> Options
        {
            get
            {
                ObservableCollection<UserControl> uc = new ObservableCollection<UserControl>();

                foreach (IMusicPlugin plugin in MusicPlugins)
                {
                    uc.Add(plugin.GetOptionsControl());
                }

                return uc;
            }
        }

        public void StartAllPlugin()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.StartPlugin();
            }
        }

        public void StopAllPlugin()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.StopPlugin();
            }
        }

        private void StartStopMusic()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.StartStopMusic();
            }
        }

        private void NextTrack()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.NextTrack();
            }
        }

        private void PreviouseTrack()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.PreviouseTrack();
            }
        }

        public void Close()
        {
            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.ClosePlugin();
            }
        }

        public void SaveSettings()
        {
            Properties.Settings.Default.Save();

            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.Save();
            }
        }

        public void ResetSettings()
        {
            Keyobserver.ResetSettings();

            foreach (IMusicPlugin plugin in MusicPlugins)
            {
                plugin.ResetSettings();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }    
    }
}
